package wujinye.com.service.impl;

import wujinye.com.dao.ArticleDao;
import wujinye.com.dao.FavoriteDao;
import wujinye.com.dao.UserDao;
import wujinye.com.dao.UserInfoDao;
import wujinye.com.model.Article;
import wujinye.com.service.ArticleService;
import wujinye.com.utils.DateUtil;
import wujinye.com.utils.StrUtil;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ArticleServiceImpl implements ArticleService {

    private ArticleDao articleDao = new ArticleDao();

    private FavoriteDao favoriteDao = new FavoriteDao();

    private UserDao userDao = new UserDao();

    @Override
    public List<Map<String, Object>> findArticleList(String search, int page,Integer uId) {
        List<Map<String, Object>> list = null;
        if (null != uId){
            list = articleDao.findMyArticleList(search,page,uId);
        }else {
            list = articleDao.findArticleList(search, page);
        }
        if (list != null) {
            for (Map<String, Object> map : list) {
                // 把数据库中datetime类型转换成String类型，并且格式化日期类型（yyyy-MM-dd HH:mm:ss）
                Timestamp publishDate = (Timestamp) map.get("publishDate");
                map.put("publishDate", DateUtil.timeToString(publishDate));
                // 把收藏数为null的改为0
                // putIfAbsent:如果传入key对应的value已经存在，就返回存在的value，不进行替换。
                // 如果不存在，就添加key和value，返回null
                map.putIfAbsent("total", 0);
            }
        }
        return list;
    }

    @Override
    public long getTotalArticle(String search,Integer uId) {
        if (null != uId){
            return articleDao.getTotalMyArticle(search,uId);
        }else {
            return articleDao.getTotalArticle(search);
        }
    }

    @Override
    public void addViews(Integer id) {
        Map<String, Object> map = articleDao.getViews(id);
        Integer views = 1;
        if (null != map) {
            String v = map.get("views").toString();
            if (StrUtil.checkString(v)) {
                views = StrUtil.stringToInteger(v) + 1;
            }
        }
        articleDao.addViews(id, views);
    }

    @Override
    public Map<String, Object> findArticleById(Integer id) {
        Map<String, Object> map = articleDao.findArticle(id);
        if (map != null) {
            // 把数据库中datetime类型转换成String类型，并且格式化日期类型（yyyy-MM-dd HH:mm:ss）
            Timestamp publishDate = (Timestamp) map.get("publishDate");
            map.put("publishDate", DateUtil.timeToString(publishDate));
            map.putIfAbsent("total", 0);
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> findCollectList(Integer uId, Integer id) {
        List<Map<String, Object>> list = favoriteDao.findCollectList(uId, id);
        if (list != null) {
            for (Map<String, Object> map : list) {
                if (!StrUtil.checkString(map.get("realName").toString())) {
                    map.put("realName", map.get("username"));
                }
            }
        }
        return list;
    }

    @Override
    public String isCollect(Integer userId, Integer aid) {
        Integer flag = favoriteDao.isCollect(userId, aid);
        if (flag != null) {
            return "1";
        }
        return "0";
    }

    @Override
    public void removeCollect(Integer uId, Integer aId) {
        favoriteDao.removeCollect(uId, aId);
    }

    @Override
    public void addCollect(Integer uId, Integer aId) {
        favoriteDao.addCollect(uId, aId);
    }

    @Override
    public void addArticle(Integer uId, String title, String content) {
        Article article = new Article();
        article.setId(null);
        article.setTitle(title);
        article.setContent(content);
        article.setBrowseCount(0);
        article.setPublishDate(new Date());
        Map<String,Object> map = userDao.getNameById(uId);
        if (null!=map){
            String realName = map.get("realName").toString();
            if (!StrUtil.checkString(realName)){
                map.put("realName",map.get("username"));
            }
        }
        article.setPublishUsername(map.get("realName").toString());
        article.setUserId(uId);
        articleDao.addArticle(article);
    }
}
